using Microsoft.AspNetCore.Mvc;
using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Services;

namespace Lzfy_Refund_Service.Controllers
{
    /// <summary>
    /// 用户修改日志控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class UumUserModifyLogController : ControllerBase
    {
        private readonly IUumUserModifyLogService _service;
        private readonly ILogger<UumUserModifyLogController> _logger;

        public UumUserModifyLogController(IUumUserModifyLogService service, ILogger<UumUserModifyLogController> logger)
        {
            _service = service;
            _logger = logger;
        }

        /// <summary>
        /// 创建用户修改日志
        /// </summary>
        /// <param name="request">创建请求</param>
        /// <returns>创建的记录ID</returns>
        [HttpPost("create")]
        public async Task<ActionResult<long>> Create([FromBody] UumUserModifyLogCreateRequest request)
        {
            try
            {
                if (request == null)
                {
                    return BadRequest("请求参数不能为空");
                }

                var id = await _service.CreateAsync(request);
                return Ok(id);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "创建用户修改日志参数错误: {Message}", ex.Message);
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建用户修改日志失败");
                return StatusCode(500, "服务器内部错误");
            }
        }

        /// <summary>
        /// 根据ID获取用户修改日志
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>用户修改日志</returns>
        [HttpPost("get-by-id")]
        public async Task<ActionResult<UumUserModifyLog>> GetById([FromBody] GetByIdRequest request)
        {
            try
            {
                if (request == null || request.Id <= 0)
                {
                    return BadRequest("ID必须大于0");
                }

                var result = await _service.GetByIdAsync(request.Id);
                if (result == null)
                {
                    return NotFound("未找到指定的用户修改日志");
                }

                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "查询用户修改日志参数错误: {Message}", ex.Message);
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询用户修改日志失败，ID: {Id}", request?.Id);
                return StatusCode(500, "服务器内部错误");
            }
        }

        /// <summary>
        /// 分页查询用户修改日志
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>分页结果</returns>
        [HttpPost("get-paged")]
        public async Task<ActionResult<UumUserModifyLogQueryResponse>> GetPaged([FromBody] UumUserModifyLogQueryRequest request)
        {
            try
            {
                if (request == null)
                {
                    request = new UumUserModifyLogQueryRequest();
                }

                var result = await _service.GetPagedAsync(request);
                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "分页查询用户修改日志参数错误: {Message}", ex.Message);
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "分页查询用户修改日志失败");
                return StatusCode(500, "服务器内部错误");
            }
        }

        /// <summary>
        /// 根据用户名获取修改日志
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>修改日志列表</returns>
        [HttpPost("get-by-username")]
        public async Task<ActionResult<IEnumerable<UumUserModifyLog>>> GetByUserName([FromBody] GetByUserNameRequest request)
        {
            try
            {
                if (request == null || string.IsNullOrWhiteSpace(request.UserName))
                {
                    return BadRequest("用户名不能为空");
                }

                var result = await _service.GetByUserNameAsync(request.UserName);
                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "根据用户名查询修改日志参数错误: {Message}", ex.Message);
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "根据用户名查询修改日志失败，用户: {UserName}", request?.UserName);
                return StatusCode(500, "服务器内部错误");
            }
        }

        /// <summary>
        /// 根据修改用户名获取修改日志
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>修改日志列表</returns>
        [HttpPost("get-by-username-modify")]
        public async Task<ActionResult<IEnumerable<UumUserModifyLog>>> GetByUserNameModify([FromBody] GetByUserNameRequest request)
        {
            try
            {
                if (request == null || string.IsNullOrWhiteSpace(request.UserName))
                {
                    return BadRequest("修改用户名不能为空");
                }

                var result = await _service.GetByUserNameModifyAsync(request.UserName);
                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "根据修改用户名查询修改日志参数错误: {Message}", ex.Message);
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "根据修改用户名查询修改日志失败，修改用户: {UserNameModify}", request?.UserName);
                return StatusCode(500, "服务器内部错误");
            }
        }

        /// <summary>
        /// 获取最近的修改日志
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>修改日志列表</returns>
        [HttpPost("get-recent")]
        public async Task<ActionResult<IEnumerable<UumUserModifyLog>>> GetRecent([FromBody] GetRecentRequest? request = null)
        {
            try
            {
                var minutes = request?.Minutes ?? 60;
                var result = await _service.GetRecentAsync(minutes);
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询最近修改日志失败，时间范围: {Minutes}分钟", request?.Minutes ?? 60);
                return StatusCode(500, "服务器内部错误");
            }
        }
    }
}